<template>
 <model-page>
	<Search :searchResult="serchText">
		<view class="controllRight">
			<view class="cancelSearch"  @click="cancelSearch">
				取消
			</view>
			<image src="../../../static/image/scan.png" mode="" class="scan"></image>
		</view>
		
	</Search>
	<view class="tabList">
		<view v-for="(item,index) in tabList" :key="index" @click="checkTabs(index)">{{item}}</view>
	</view>
	<BuildIng @confirm="confirm" :formData="postData"/>
	<view class="tables">
		<Table
		:headList="headList" 
		:tableData="tableData"
		:stateList="stateList"
		 :paging="false"
		></Table>
	</view>
 </model-page>
</template>

<script setup>
	import {reactive, ref} from 'vue'
	import Search from '@/component/Search.vue'
	import Table from '../../../component/Table.vue';
	import BuildIng from '@/component/Builing.vue'
	import {onLoad} from '@dcloudio/uni-app'
	import {listVisitForGuard} from '@/api/index.js'
	const tabList = ["首次未录入","已录入"]
	const checkTabs = (index)=>{
		let url = ""
		switch(index){
			case 0:
			url = '/packageDataInt/pages/notEntered/notEntered'
			break;
			case 1:
			url = '/packageDataInt/pages/entered/entered'
			break;
		}
		uni.navigateTo({
			url
		})
	}
	const stateList  = [
		{
			id:1,
			label:"详情",
			color:"#2778FF",
			path:"/packageDataInt/pages/dataIntDetail/dataIntDetail"
		}
	]
	const headList = ref([
		{
			title:"所属楼栋",
			props:"buildingName",
			width:"18%"
		},
		{
			title:"所属楼层",
			props:"floorName",
			width:"18%"
		},
		{
			title:"房间号",
			props:"roomNumber",
			width:"14%"
		},
		{
			title:"床号",
			props:"bedNumber",
			width:"18%"
		},
		{
			title:"老人名",
			props:"elderName",
			width:"12%"
		},
		{
			title:"操作",
			props:"nullState",
			width:"10%"
		},
	])
	const tableData = ref([])
	const postData = ref({
		buildId:"",
		floorId:"",
		roomId:""
	})
	//根据楼栋查询老人列表
	const confirm = async(e)=>{
		postData.value.buildId = e.buildId
		postData.value.floorId = e.floorId
		postData.value.roomId = e.roomId
		resetGetList()
	}
	// 查询列表
	const getFilterList = async()=>{
		const {code,data:res} = await listVisitForGuard(postData.value)
		if(code === 200){
			if(res.length > 0){
				tableData.value = res.map(val=>{
					return Object.assign(val,{nullState:"1"})
				})
			}else{
				tableData.value = []
			}
		}
	}
	// 搜索
	const serchText = ref("")
	const cancelSearch = ()=>{
		serchText.value = ""
		delete postData.value.elderId
		resetGetList()
	}
	const resetGetList = ()=>{
		const {buildId,floorId,roomId,elderId} = postData.value
		if(buildId || floorId || roomId){
			getFilterList()
		}else if(elderId){
			
		}
		else{
				tableData.value = []
		}
	}
	onLoad(()=>{
		uni.$on("undateElderId",(option)=>{
			const {type,searchResult} = option
			serchText.value = searchResult || ""
			postData.value.elderId = type
			postData.value.buildId = ""
			postData.value.floorId = ""
			postData.value.roomId = ""
			getFilterList()
		})
	})
</script>

<style lang="less" scoped>
	.scan{
		width: 40rpx;
		height: 40rpx;
	}
	.tabList{
		display: flex;
		justify-content: space-between;
		>view{
			padding: 32rpx 0;
			font-weight: 500;
			font-size: 28rpx;
			color: #666666;
		}
	}
	.tables{
		margin-top: 25rpx;
	}
	.controllRight{
		display: flex;
		.cancelSearch{
			width: 80rpx;
			margin-right: 20rpx;
		}
	}

</style>